package com.clhbolg.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.clhbolg.domain.bo.ArticleTagBo;
import com.clhbolg.domain.vo.ArticleTagVo;
import com.clhbolg.service.IArticleTagService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 标签关联关系
 *
 * @author cailinhu
 * @date 2023-02-08
 */
@Validated
//标记为final的对象,会自动进行注入
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/articleTag")
public class ArticleTagController extends BaseController {

    private final IArticleTagService iArticleTagService;

    /**
     * 查询【标签关联关系】列表
     */
    @SaCheckPermission("system:articleTag:list")
    @GetMapping("/list")
    public TableDataInfo<ArticleTagVo> list(ArticleTagBo bo, PageQuery pageQuery) {
        return iArticleTagService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出【标签关联关系】列表
     */
    @SaCheckPermission("system:articleTag:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ArticleTagBo bo, HttpServletResponse response) {
        List<ArticleTagVo> list = iArticleTagService.queryList(bo);
        ExcelUtil.exportExcel(list, "【请填写功能名称】", ArticleTagVo.class, response);
    }

    /**
     * 获取【标签关联关系】详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:articleTag:query")
    @GetMapping("/{id}")
    public R<ArticleTagVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iArticleTagService.queryById(id));
    }

    /**
     * 新增标签关联关系
     */
    @SaCheckPermission("system:articleTag:add")
    @Log(title = "【新增标签关联关系】", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ArticleTagBo bo) {
        return toAjax(iArticleTagService.insertByBo(bo));
    }

    /**
     * 修改标签关联关系
     */
    @SaCheckPermission("system:articleTag:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ArticleTagBo bo) {
        return toAjax(iArticleTagService.updateByBo(bo));
    }

    /**
     * 删除标签关联关系
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:articleTag:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iArticleTagService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
